package com.example.endusation.service.impl;

import com.example.endusation.dao.CourseDAO;
import com.example.endusation.dto.OptionsDTO;
import com.example.endusation.dto.UserInfoDTO;
import com.example.endusation.entity.ChapterDO;
import com.example.endusation.entity.CourseDO;
import com.example.endusation.entity.ExercisesDO;
import com.example.endusation.entity.LessonDO;
import com.example.endusation.service.CourseService;
import com.example.endusation.util.WrapperResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDAO courseDAO;
    @Override
    public WrapperResponse getCourse(UserInfoDTO userInfoDTO) {
        List<CourseDO> list = courseDAO.getCourse(userInfoDTO.getUsername());
        return WrapperResponse.success(list);
    }

    @Override
    public WrapperResponse getCourseDetil(String courseId) {
        List<ChapterDO> list = courseDAO.getChapter(courseId);
        for(ChapterDO chapterDO:list){
            List<LessonDO> list1 = courseDAO.getLesson(chapterDO.getId());
            for(LessonDO lessonDO:list1){
                List<ExercisesDO> list2 = courseDAO.getExercise(lessonDO.getId());
                for(ExercisesDO exercisesDO:list2){
                    List<OptionsDTO> list3 = courseDAO.getOptions(exercisesDO.getId());
                    exercisesDO.setOptions(list3);
                }
                lessonDO.setExercises(list2);
            }
            chapterDO.setLessons(list1);
        }
        CourseDO courseDO = new CourseDO();
        courseDO.setChapters(list);
        return WrapperResponse.success(courseDO);
    }
}
